import {getCategoryList, getBankList, getLikeList, getBrandList, postSearch,getGoodsInfoById} from '@/api/product';
export default {
    namespaced:true,
    state:{
        categoryList:[],
        bankList:[],
        likeInfo:{
            pageNo:1,
            pageSum:1,
            likeList:[]
        },
        brandList:[],
        searchResult:{},
        // 商品详情信息
        goodsInfo:{
            // 类别
            categoryView:{},
            // 详情
            skuInfo:{},
            // 销售属性
            spuSaleAttrList:{}
        }
    },
    mutations:{
        SET_CATEGORY_LIST(state,categoryList){
            state.categoryList = categoryList;
        },
        SET_BANK_LIST(state,bankList){
            state.bankList = bankList;
        },
        SET_LIKE_INFO(state,likeInfo){
            state.likeInfo = likeInfo;
        },
        SET_BRAND_LIST(state,brandList){
            state.brandList= brandList;
        },
        SET_SEARCH_RESULT(state,searchResult){
            state.searchResult= searchResult;
        },
        SET_GOODS_INFO(state,payload){
            state.goodsInfo = payload;
        },
        SET_DEFAULT_IMG(state,id){
            state.goodsInfo.skuInfo.imgs = state.goodsInfo.skuInfo.imgs.map(item=>{
                item.default = false;
                if(item.id === id){
                    item.default = true;
                    state.goodsInfo.skuInfo.defaultImg = item.url
                }
                return item
            })
        }
    },
    actions:{
        async getCategoryListAsync({commit},limit){
            const {categoryList} = await getCategoryList(limit);
            commit('SET_CATEGORY_LIST',categoryList);
        },
        async getBankListAsync({commit}, {type,limit}){
            const {skuList} = await getBankList(type,limit);
            commit('SET_BANK_LIST',skuList);
        },
        async getLikeListAsync({commit,state}, {pageNo=1,pangSize}={}){
            if(pageNo>state.likeInfo.pageSum) pageNo=1;
            const {likeList,pageSum} = await getLikeList(pageNo,pangSize);
            commit('SET_LIKE_INFO', {
                likeList,
                pageSum,
                pageNo
            });
        },
        async getBrandListAsync({commit}, {limit}={}){
            const {trademarkList} = await getBrandList(limit);
            commit('SET_BRAND_LIST',trademarkList);
        },
        async getSearchResultAsync({commit}, data){
            const response = await postSearch(data);
            commit('SET_SEARCH_RESULT',response.data);
        },
        async getGoodsInfoByIdAsync({commit},id){
            const response = await getGoodsInfoById(id);
            commit("SET_GOODS_INFO",response.data);
        }
    },
    getters:{
        categoryView({goodsInfo:{categoryView}}){
            return new Array(3).fill(undefined).map(function(_,index){
                const level = index+1;
                return {
                    name:categoryView["category"+level+"Name"],
                    id:categoryView["category"+level+"Id"],
                    level
                }
            });
        }
    }
}